<div class="layui-card">
	<div class="layui-card-header">
		<h2 class="header-title">活动详情</h2>
		<span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>活动详情</cite></a>
        </span>
	</div>
</div>
<div class="layui-form toolbar">
	<button id="add_a" class="layui-btn icon-btn">添加活动</button>
</div><br />
<div class="layui-fluid layadmin-cmdlist-fluid zbyad">
	<div class="layui-row layui-col-space10" id="ads_list">
	</div>
	<i class="layui-clear"></i>
	<!--<div class="layui-col-md12 layui-col-sm12">
		<div id="demo0">
			<div class="layui-box layui-laypage layui-laypage-default" id="layui-laypage-1">
				<a href="javascript:;" class="layui-laypage-prev layui-disabled" data-page="0">上一页</a><span class="layui-laypage-curr"><em class="layui-laypage-em"></em><em>1</em></span>
				<a href="javascript:;" data-page="2">2</a>
				<a href="javascript:;" data-page="3">3</a>
				<a href="javascript:;" data-page="4">4</a>
				<a href="javascript:;" data-page="5">5</a>
				<a href="javascript:;" class="layui-laypage-next" data-page="2">下一页</a>
			</div>
		</div>
	</div>-->
</div>

<!-- 表单弹窗 -->
<script type="text/html" id="ads-model">
	<form id="ads-form" lay-filter="ads-form" class="layui-form model-form zfrom" enctype="multipart/form-data">
		<input name="userIdCreate" type="hidden" id="asid" />

		<div class="layui-row layui-col-space10" id="ads_list">
			<div class="layui-col-md6 layui-col-sm6">
				<label class="layui-form-label">选择图片</label>
				<div class="hdImgBtn" id="a1" onchange="upload()">
					<input class="uploadImg" type="file" name="picUrl" id="mu">
				</div>
			</div>
			<div class="layui-col-md6 layui-col-sm6">
				<div class="layui-form-item">
					<label class="layui-form-label">活动开始时间</label><br />
					<div class="">
						<input type="data" class="layui-input" name="start" id="test1" placeholder="yyyy-MM-dd">
					</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label">活动结束时间</label><br />
					<div class="">
						<input type="data" class="layui-input" name="end" id="test2" placeholder="yyyy-MM-dd">
					</div>
				</div>
			</div>
		</div>
		<i class="layui-clear"></i>
		<div class="layui-form-item model-form-footer">
			<button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
			<button class="layui-btn" lay-filter="ad-form-submit" lay-submit>保存</button>
		</div>
	</form>

</script>

<script>
	layui.use(['laydate', 'table', 'layer', 'form', 'util', 'config', 'index', 'admin'], function() {
		var laydate = layui.laydate;
		var form = layui.form;
		var table = layui.table;
		var config = layui.config;
		var util = layui.util;
		var com = layui.common;
		var index = layui.index;
		var admin = layui.admin;
		var alog = config.getTempData('loginInfo');
		var curTime = new Date().getFullYear() + "-" + (new Date().getMonth() + 1) + "-" + new Date().getDate() + " " + new Date().getHours() + ":" + new Date().getMinutes() + ":" + new Date().getSeconds();
		var ds;

		window.updataAd = function(id) {
			showAdsModel(JSON.parse(id))
		}

		window.delAd = function(id) {
			layer.confirm('确定移出此活动吗？', function(i) {
				$.ajax({
					type: "POST",
					url: delAds,
					data: {
						adId: id
					},
					dataType: "json",
					xhrFields: {
						withCredentials: true
					},
					success: function(data) {
						layer.msg(data.msg, {
							icon: 1
						});
						userListV()
					},
					error: function(result) {
						console.log(result);
					}
				});
			});
		}

		$("#add_a").click(function() {
			showAdsModel()
		})
		var paths = ''
		window.upload = function() {
			var self = this;
			$.ajax({
				url: uploadHead,
				type: "post",
				dataType: "json",
				cache: false,
				data: new FormData($("#ads-form")[0]),
				xhrFields: {
					withCredentials: true
				},
				processData: false, // 不处理数据 
				contentType: false, // 不设置内容类型 
				success: function(data) { /* 后端返回的数据格式为 {"url": "xxxxxx"} */
					paths = data.rows;
					$('#a1').css("background-image", "url(" + data.rows + ")")
				}
			})
		}

		var showAdsModel = function(data) {
			console.log(data)
			layer.open({
				type: 1,
				title: data ? '修改活动' : '添加活动',
				area: '650px',
				offset: '80px',
				skin: 'zbyjs',
				content: $('#ads-model').html(),
				success: function() {
					$('#ads-form')[0].reset();
					$('#ads-form').attr('method', 'GET');
					var s, d
					if(data) {
						s = data.startTime
						d = data.endTime
						$('#a1').css("background-image", "url(" + data.activePicture + ")")
						$("#asid").val(data.adId);
					} else {
						s = curTime
						d = curTime
					}
					laydate.render({
						elem: '#test1',
						value: s,
						type: "datetime",
						done: function(value, date) {}
					});
					laydate.render({
						elem: '#test2',
						value: d,
						type: "datetime",
						done: function(value, date) {}
					});
				}
			});
		};

		form.on('submit(ad-form-submit)', function(data) {
			console.log(data)
			console.log(paths)
			var hhh = $(".zbyjs .layui-layer-title").text();
			if(hhh == "添加活动") {
				chuli(addAd, {
					activePicture: paths,
					startTime: data.field.start,
					endTime: data.field.end
				});
			} else if(hhh == "修改活动") {
				chuli(updateAd, {
					adId: data.field.userIdCreate,
					activePicture: paths,
					startTime: data.field.start,
					endTime: data.field.end
				});
			}
			return false;
		});

		function chuli(apath, datas) {
			$.ajax({
				type: "POST",
				url: apath,
				data: {
					params: JSON.stringify(datas)
				},
				dataType: "json",
				xhrFields: {
					withCredentials: true
				},
				success: function(data) {
					layer.closeAll('loading');
					layer.msg(data.msg, {
						icon: 1
					});
					userListV()
					$('#user-form').attr('method')
				},
				error: function(result) {
					console.log(result);
				},
			});
		}

	})

	//1125 × 2436
	function showaList(adlist) {
		var zbylist = "";
		for(var i = 0; i < adlist.length; i++) {
			ds = JSON.stringify(adlist[i])
			zbylist += '<div class="layui-col-xs6 layui-col-md3">' +
				'<div class="cmdlist-container layui-card layui-card-body zbycon">' +
				'<a href="javascript:;"><img src="' + adlist[i].activePicture + '"></a>' +
				'<a href="javascript:;">' +
				'<p>开始时间：' + adlist[i].startTime + '</p>' +
				'<p>结束时间：' + adlist[i].endTime + '</p>' +
				'<div><button class="layui-btn" onclick="updataAd(ds)">编辑</button>' +
				'<button class="layui-btn layui-btn-danger" onclick="delAd(' + adlist[i].adId + ')">删除</button></div>' +
				'</a></div></div>';
		}
		$("#ads_list").html(zbylist)
	}
	userListV()

	function userListV() {

		$.ajax({
			type: "POST",
			url: getAds,
			data: {
				params: JSON.stringify({
					pageSize: 1000,
					pageNumber: 1,
				})
			},
			dataType: "json",
			xhrFields: {
				withCredentials: true
			},
			beforeSend: function(request) {},
			success: function(data) {
				console.log(data)
				showaList(data.rows)
			},
			beforeSend: function() {},
			complete: function() {},
			error: function(result) {
				console.log(result);
				config.removeToken();
				location.replace('login.html');
			},
		});
	}
</script>